package mof.gov.dmdc.view.searchadapter;

import java.text.DateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.event.ActionEvent;

import mof.gov.dmdc.view.searchitem.SimpleSearchCriteria;

import oracle.jbo.Row;
import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;

public class SimpleSearchAdapter extends SearchAdapter implements SearchAdapterInterface {
    public SimpleSearchAdapter() {
        super();
    }
    private List<SimpleSearchCriteria> model;

    @Override
    public void searchAction(ActionEvent event) {
        if (!model.isEmpty()) {
            ViewObject vo = getViewObject();
            ViewCriteria oldCriteria;
            try {
                oldCriteria = vo.getViewCriteria();
            } catch (Exception e) {
                oldCriteria = null;
            }
            if (oldCriteria != null) {
                oldCriteria.clear();
            }
            ViewCriteria criteria = vo.createViewCriteria();
            for (SimpleSearchCriteria item : model) {
                ViewCriteriaRow criteriaRow = criteria.createViewCriteriaRow();
                String value;
                String type;
                try {
                    value = item.getAttrValue().toString();
                } catch (Exception e) {
                    value = "";
                }
                try {
                    type = item.getAttrType();
                } catch (Exception e) {
                    type = "";
                }
                if (value != "") {
                    if (item.getAttrName().equalsIgnoreCase("ten")) {
                        criteriaRow.setAttribute(item.getAttrName(), "%" + value.toUpperCase().trim() + "%");
                        criteriaRow.setOperator(item.getAttrName(), "like");
                        criteriaRow.setUpperColumns(true);
                    } else {
                        if (type.equalsIgnoreCase("date")) {
                            DateFormat formater = new SimpleDateFormat("dd/MM/yyyy");
                            Date date = null;
                            try {
                                date = formater.parse(value);
                            } catch (ParseException e) {
                                System.out.println("Error parse date Time!");
                            }
                            criteriaRow.setAttribute(item.getAttrName(), date);
                            criteriaRow.setOperator(item.getAttrName(), "=");
                        } else {
                            criteriaRow.setAttribute(item.getAttrName(),"%" + item.getAttrValue().toUpperCase().trim()+"%");
                            criteriaRow.setOperator(item.getAttrName(), "like");
                            criteriaRow.setUpperColumns(true);
                        }
                    }
                    criteriaRow.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
                    criteria.addRow(criteriaRow);
                }
            }
            if (criteria.getRowCount() != 0) {
                vo.applyViewCriteria(criteria);
                //vo.setMaxFetchSize(-1);
                //vo.executeQuery();
                //vo.clearCache();
                //vo.setMaxFetchSize(maxFletSize);
                vo.executeQuery();
                vo.clearCache();
                currenTotalSize = vo.getEstimatedRowCount();
            } 
            /*else {
                vo.getViewCriteria().clear();
                //vo.applyViewCriteria(criteria);
                //vo.setMaxFetchSize(-1);
                //vo.executeQuery();
                //vo.clearCache();
                vo.setMaxFetchSize(maxFletSize);
                vo.executeQuery();
                vo.clearCache();
                currenTotalSize = vo.getEstimatedRowCount();
            //}*/            
            refreshPage();
        }
    }

    @Override
    public void resetAction(ActionEvent event) {
        for (SimpleSearchCriteria criteria : model) {
            criteria.setAttrValue("");
        }
        ViewObject vo = getViewObject();
        ViewCriteria criteria;
        try {
            criteria = vo.getViewCriteria();
        } catch (Exception e) {
            criteria = null;
        }
        if (criteria != null && !criteria.isEmpty()) {
            vo.getViewCriteria().clear();
            //vo.setMaxFetchSize(-1);
            //vo.executeQuery();
            //vo.clearCache();
            //vo.setMaxFetchSize(50);
            maxFletSize = 50;
            vo.executeQuery();
            vo.clearCache();
            //currenTotalSize = vo.getEstimatedRowCount();
        }
    }

    public void setModel(List<SimpleSearchCriteria> model) {
        this.model = model;
    }

    public List<SimpleSearchCriteria> getModel() {
        if (model == null || model.size() == 0) {
            ViewObject vo = getViewSearchField();
            model = new ArrayList<SimpleSearchCriteria>();
            do {
                Row row = vo.hasNext() ? vo.next() : vo.first();
                String attrName = "";
                String attrlabel = "";
                String lovName = "";
                String attrType = "";
                if (row != null) {
                    try {
                        attrName = row.getAttribute("ColName").toString();
                    } catch (NullPointerException e) {
                        attrName = "";
                    }
                    try {
                        attrlabel = row.getAttribute("LabelSearch").toString();
                    } catch (NullPointerException e) {
                        attrlabel = "";
                    }
                    try {
                        lovName = row.getAttribute("LovName").toString();
                    } catch (NullPointerException e) {
                        lovName = "";
                    }
                    try {
                        attrType = row.getAttribute("ColType").toString();
                    } catch (NullPointerException e) {
                        attrType = "";
                    }
                }
                SimpleSearchCriteria crieteria = new SimpleSearchCriteria(attrName, attrlabel, null, attrType, lovName);
                model.add(crieteria);
            } while (vo.hasNext());
        }
        return model;
    }
}
